﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Enums;

namespace ProgNetComponentsDx.Browsers.Design
{
    public class BrowserColumnTypeConverter : ProgNetComponents.Design.TypeConverters.CollectionItemTypeConverter<BrowserColumn>
    {
        protected override System.Reflection.ConstructorInfo GetConstructor(BrowserColumn item)
        {
            #region default
            if (item.BrowserId.IsNull() &&
                item.Caption.IsNull() &&
                item.ColumnType == BrowserColumnType.TextEdit &&
                item.DefaultValue.IsNull() &&
                item.Expression.IsNull() &&
                item.FilterType == Enums.FilterType.BeginsWith &&
                item.IsSearchField &&
                item.Name.IsNull() &&
                item.Visible &&
                item.Width == 100)
            {
                return typeof(BrowserColumn).GetConstructor(Type.EmptyTypes);
            }
            #endregion
            #region name
            if (item.BrowserId.IsNull() &&
                item.Caption.IsNull() &&
                item.ColumnType == BrowserColumnType.TextEdit &&
                item.DefaultValue.IsNull() &&
                item.Expression.IsNull() &&
                item.FilterType == Enums.FilterType.BeginsWith &&
                item.IsSearchField &&
                item.Visible &&
                item.Width == 100)
            {
                return typeof(BrowserColumn).GetConstructor(new Type[] { typeof(string) });
            }
            #endregion
            #region name, filter
            if (item.BrowserId.IsNull() &&
               item.Caption.IsNull() &&
               item.ColumnType == BrowserColumnType.TextEdit &&
               item.DefaultValue.IsNull() &&
               item.Expression.IsNull() &&
               item.IsSearchField &&
               item.Visible &&
               item.Width == 100)
            {
                return typeof(BrowserColumn).GetConstructor(new Type[] { typeof(string), typeof(FilterType) });
            }
            #endregion
            #region name, filter, visible
            if (item.BrowserId.IsNull() &&
               item.Caption.IsNull() &&
               item.ColumnType == BrowserColumnType.TextEdit &&
               item.DefaultValue.IsNull() &&
               item.Expression.IsNull() &&
               item.IsSearchField &&
               item.Width == 100)
            {
                return typeof(BrowserColumn).GetConstructor(new Type[] { typeof(string), typeof(FilterType) , typeof(bool)});
            }
            #endregion
            #region name, filter, type
            if (item.BrowserId.IsNull() &&
               item.Caption.IsNull() &&
               item.DefaultValue.IsNull() &&
               item.Expression.IsNull() &&
               item.IsSearchField &&
               item.Visible &&
               item.Width == 100 && 
               ((item.ColumnType == BrowserColumnType.CheckEdit && item.ValueChecked.EqualsSafe(true) && item.ValueUnchecked.EqualsSafe(false)) ||
                (item.ValueChecked.IsNull() && item.ValueUnchecked.IsNull() && item.BrowserId.IsNull()))
               )
            {
                return typeof(BrowserColumn).GetConstructor(new Type[] { typeof(string), typeof(FilterType), typeof(BrowserColumnType) });
            }
            #endregion
            #region name, filter, type, visible
            if (item.BrowserId.IsNull() &&
               item.Caption.IsNull() &&
               item.DefaultValue.IsNull() &&
               item.Expression.IsNull() &&
               item.IsSearchField &&
               item.Width == 100 &&
               ((item.ColumnType == BrowserColumnType.CheckEdit && item.ValueChecked.EqualsSafe(true) && item.ValueUnchecked.EqualsSafe(false)) ||
                (item.ValueChecked.IsNull() && item.ValueUnchecked.IsNull() && item.BrowserId.IsNull()))
               )
            {
                return typeof(BrowserColumn).GetConstructor(new Type[] { typeof(string), typeof(FilterType), typeof(BrowserColumnType) , typeof(bool)});
            }
            #endregion
            #region name, caption, expression
            if (item.BrowserId.IsNull() &&
                 item.ColumnType == BrowserColumnType.TextEdit &&
                 item.DefaultValue.IsNull() &&
                 item.FilterType == Enums.FilterType.BeginsWith &&
                 item.IsSearchField &&
                 item.Visible &&
                 item.Width == 100)
            {
                return typeof(BrowserColumn).GetConstructor(new Type[] { typeof(string), typeof(string), typeof(string) });
            }
            #endregion
            #region name, visible, issearchfield
            if (item.BrowserId.IsNull() &&
                item.Caption.IsNull() &&
                item.ColumnType == BrowserColumnType.TextEdit &&
                item.DefaultValue.IsNull() &&
                item.Expression.IsNull() &&
                item.FilterType == Enums.FilterType.BeginsWith &&
                item.Width == 100)
            {
                return typeof(BrowserColumn).GetConstructor(new Type[] { typeof(string), typeof(bool), typeof(bool) });
            }
            #endregion
            #region name, type, defaultValue, ValueChecked, ValueUnchecked, BrowserId
            if (item.Caption.IsNull() &&
                item.Expression.IsNull() &&
                item.FilterType == Enums.FilterType.BeginsWith &&
                item.IsSearchField &&
                item.Visible &&
                item.Width == 100)
            {
                return typeof(BrowserColumn).GetConstructor(new Type[] { typeof(string),typeof(BrowserColumnType), 
                    typeof(object),typeof(object),typeof(object),typeof(object) });
            }
            #endregion

            return typeof(BrowserColumn).GetConstructor(new Type[] {
                typeof(string),
                typeof(string),
                typeof(string),
                typeof(FilterType),
                typeof(bool),
                typeof(bool), 
                typeof(int), 
                typeof(BrowserColumnType), 
                typeof(object), 
                typeof(object), 
                typeof(object), 
                typeof(object)});
        }

        protected override object[] GetConstructorParamters(BrowserColumn item)
        {
            #region default
            if (item.BrowserId.IsNull() &&
                item.Caption.IsNull() &&
                item.ColumnType == BrowserColumnType.TextEdit &&
                item.DefaultValue.IsNull() &&
                item.Expression.IsNull() &&
                item.FilterType == Enums.FilterType.BeginsWith &&
                item.IsSearchField &&
                item.Name.IsNull() &&
                item.Visible &&
                item.Width == 100)
            {
                return new object[0];
            }
            #endregion
            #region name
            if (item.BrowserId.IsNull() &&
                item.Caption.IsNull() &&
                item.ColumnType == BrowserColumnType.TextEdit &&
                item.DefaultValue.IsNull() &&
                item.Expression.IsNull() &&
                item.FilterType == Enums.FilterType.BeginsWith &&
                item.IsSearchField &&
                item.Visible &&
                item.Width == 100)
            {
                return new object[] { item.Name };
            }
            #endregion
            
            #region name,filter
            if (item.BrowserId.IsNull() &&
              item.Caption.IsNull() &&
              item.ColumnType == BrowserColumnType.TextEdit &&
              item.DefaultValue.IsNull() &&
              item.Expression.IsNull() &&
              item.IsSearchField &&
              item.Visible &&
              item.Width == 100)
            {
                return new object[] { item.Name, item.FilterType };
            }

            #endregion
            #region name,filter, visible
            if (item.BrowserId.IsNull() &&
              item.Caption.IsNull() &&
              item.ColumnType == BrowserColumnType.TextEdit &&
              item.DefaultValue.IsNull() &&
              item.Expression.IsNull() &&
              item.IsSearchField &&
              item.Width == 100)
            {
                return new object[] { item.Name, item.FilterType, item.Visible };
            }

            #endregion
            #region name, filter, type
            if (item.BrowserId.IsNull() &&
              item.Caption.IsNull() &&
              item.DefaultValue.IsNull() &&
              item.Expression.IsNull() &&
              item.IsSearchField &&
              item.Visible &&
              item.Width == 100 &&
              ((item.ColumnType == BrowserColumnType.CheckEdit && item.ValueChecked.EqualsSafe(true) && item.ValueUnchecked.EqualsSafe(false)) ||
               (item.ValueChecked.IsNull() && item.ValueUnchecked.IsNull() && item.BrowserId.IsNull()))
              )
            {
                return new object[] { item.Name, item.FilterType, item.ColumnType };
            }
            #endregion
            #region name, filter, type, visible
            if (item.BrowserId.IsNull() &&
              item.Caption.IsNull() &&
              item.DefaultValue.IsNull() &&
              item.Expression.IsNull() &&
              item.IsSearchField &&
              item.Width == 100 &&
              ((item.ColumnType == BrowserColumnType.CheckEdit && item.ValueChecked.EqualsSafe(true) && item.ValueUnchecked.EqualsSafe(false)) ||
               (item.ValueChecked.IsNull() && item.ValueUnchecked.IsNull() && item.BrowserId.IsNull()))
              )
            {
                return new object[] { item.Name, item.FilterType, item.ColumnType, item.Visible };
            }
            #endregion
            #region name, caption, expression
            if (item.BrowserId.IsNull() &&
                 item.ColumnType == BrowserColumnType.TextEdit &&
                 item.DefaultValue.IsNull() &&
                 item.FilterType == Enums.FilterType.BeginsWith &&
                 item.IsSearchField &&
                 item.Visible &&
                 item.Width == 100)
            {
                return new object[] { item.Name, item.Caption, item.Expression };
            }
            #endregion
            #region name, visible, isSearchField
            if (item.BrowserId.IsNull() &&
                item.Caption.IsNull() &&
                item.ColumnType == BrowserColumnType.TextEdit &&
                item.DefaultValue.IsNull() &&
                item.Expression.IsNull() &&
                item.FilterType == Enums.FilterType.BeginsWith &&
                item.Width == 100)
            {
                return new object[] { item.Name, item.Visible, item.IsSearchField };
            }
            #endregion
            #region name, type, defaultValue, ValueChecked, ValueUnchecked, BrowserId
            if (item.Caption.IsNull() &&
                item.Expression.IsNull() &&
                item.FilterType == Enums.FilterType.BeginsWith &&
                item.IsSearchField &&
                item.Visible &&
                item.Width == 100)
            {
                return new object[] { item.Name, item.ColumnType, item.DefaultValue, item.ValueChecked, item.ValueUnchecked, item.BrowserId };
            }
            #endregion
            return new object[] {
            item.Name, 
            item.Caption, 
            item.Expression, 
            item.FilterType,
            item.IsSearchField, 
            item.Visible, 
            item.Width, 
            item.ColumnType, 
            item.DefaultValue, 
            item.ValueChecked, 
            item.ValueUnchecked,
            item.BrowserId};
        }
    }
}
